package com.example.day1030_standardevents.slice;

import com.example.day1030_standardevents.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;
import ohos.multimodalinput.event.KeyEvent;
import ohos.multimodalinput.standard.KeyEventHandle;
import ohos.multimodalinput.standard.MultimodalEventHandle;

public class TestBackSlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_test_back);

        MultimodalEventHandle.registerStandardizedEventHandle(getAbility(), keyEvents);
    }
    KeyEventHandle keyEvents = new KeyEventHandle() {
        @Override
        public boolean onKey(KeyEvent keyEvent) {
            if (keyEvent.isKeyDown()){
                if (keyEvent.getKeyCode() == keyEvent.KEY_BACK){
                    new ToastDialog(getContext()).setText("按下了返回按钮").show();
                }
            }


            return false;
        }
    };

    @Override
    protected void onStop() {
        MultimodalEventHandle.unregisterStandardizedEventHandle(getAbility(), keyEvents);
        super.onStop();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
